package com.pod5.skype.single;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
public class ModifyXMLFile {
 
private LinkedList<String> rawIPData;
private LinkedList<String> readableIP;
private LinkedList<String> crawledIP;
private String filepath;
private CrawledIPState stateIP;


public ModifyXMLFile(String filePath) {
	// TODO Auto-generated method stub
	stateIP = new CrawledIPState();
	this.filepath = filePath;
	
}

private String parseAddressIP(String s)
{
	String result = Integer.parseInt(s.substring(0, 2),16)
						+"."+Integer.parseInt(s.substring(2, 4),16)
						+"."+Integer.parseInt(s.substring(4, 6),16)
						+"."+Integer.parseInt(s.substring(6, 8),16);
	return result;
}
 
 
private void parseHostCache(String textContent) {
	// TODO Auto-generated method stub
	String[] splitAddr = textContent.split("50041050200");
	
	for(int i =1; i < splitAddr.length; i++)
	{
		rawIPData.add(splitAddr[i]);
		String addressIP = parseAddressIP(splitAddr[i]);
		if(!readableIP.contains(addressIP))
			readableIP.add(addressIP);
	}
	
} 



	//Built for one machine parsing and storing information.
 public void modifySkypeHostCache() {
	
	 //Load previous state.
	 try{
			 rawIPData = stateIP.loadState("rawIPData");
			 readableIP = stateIP.loadState("readableIP");
			 crawledIP = stateIP.loadState("crawledIP");
	 }
	 catch(DataTypeException e){
		 
	 }

   try{
	 
	 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 Document doc = docBuilder.parse(filepath);
 
	 //Get the root element
	 Node company = doc.getFirstChild(); 
 
	  
         //Get the staff element by tag name directly
         Node connection = doc.getElementsByTagName("Connection").item(0);
 
    	 NodeList list = connection.getChildNodes();
    	 
    	 for (int i =0; i<list.getLength();i++){
    		 Node node = list.item(i);
     
    		 //get the salary element, and update the value    		 
    		 if("HostCache".equals(node.getNodeName())){
    			 parseHostCache(node.getTextContent());
    			 if(!rawIPData.isEmpty())
    			 {
    				 while(crawledIP.contains(parseAddressIP(rawIPData.getFirst())))
    				 {
    					 rawIPData.removeFirst();
    				 }
    				 crawledIP.add(parseAddressIP(rawIPData.getFirst()));
    				 node.setTextContent("41C801050041050041050200" + rawIPData.getFirst());
    				 try{
	    				 stateIP.saveState(rawIPData, "rawIPData");
	    				 stateIP.saveState(readableIP, "readableIP");
	    				 stateIP.saveState(crawledIP, "crawledIP");
    				 }catch(DataTypeException e)
    				 {
    					 
    				 }
    				
    				 
    			 }
    		 }
    	 }
 

     //write the content into xml file
     TransformerFactory transformerFactory = TransformerFactory.newInstance();
     Transformer transformer = transformerFactory.newTransformer();
     DOMSource source = new DOMSource(doc);
     StreamResult result =  new StreamResult(new File(filepath));
     transformer.transform(source, result);
 
     System.out.println("Done");
 
   }catch(ParserConfigurationException pce){
	 pce.printStackTrace();
   }catch(TransformerException tfe){
	 tfe.printStackTrace();
   }catch(IOException ioe){
	 ioe.printStackTrace();
   }catch(SAXException sae){
	 sae.printStackTrace();
   }
   
  
 }





}
